<script>
    $(function() {
        $( ".datepicker" ).datepicker();
    });
    function days_between(date1, date2) {

        // The number of milliseconds in one day
        var ONE_DAY = 1000 * 60 * 60 * 24

        // Convert both dates to milliseconds
        var date1_ms = date1.getTime()
        var date2_ms = date2.getTime()

        // Calculate the difference in milliseconds
        var difference_ms = Math.abs(date1_ms - date2_ms)

        // Convert back to days and return
        return Math.round(difference_ms/ONE_DAY)

    }
    //        function arrayCompare(a1, a2) {
    //            if (a1.length != a2.length) return false;
    //            var length = a2.length;
    //            for (var i = 0; i < length; i++) {
    //                if (a1[i] !== a2[i]) return false;
    //            }
    //            return true;
    //        }
    //
    //        function inArray(needle, haystack) {
    //            var length = haystack.length;
    //            for(var i = 0; i < length; i++) {
    //                if(typeof haystack[i] == 'object') {
    //                    if(arrayCompare(haystack[i], needle)) return true;
    //                } else {
    //                    if(haystack[i] == needle) return true;
    //                }
    //            }
    //            return false;
    //        }

        
    $(document).ready(function(){
        var id_kamar = $( "#id-kamar" ).val();
        var temp2 = new Array();
        $("#jenis-pengenal").change(function() {
            $.post("<?php echo DATACUSTOMER ?>get_data_customer_autocomplete", "no_pengenal="+$("#no-pengenal").val()+"&&jenis_pengenal="+$("#jenis-pengenal").val(), function(data){
                var arr = $.parseJSON(data);
                if (arr['no_pengenal']!='')
                {
                    $("#country-of-passport").val(arr['country_of_passport']);
                    $("#first-name").val(arr['first_name']);
                    $("#last-name").val(arr['last_name']);
                    $("#status-data-customer").val(arr['id']);
                }
                else
                    $("#status-data-customer").val('-');
            });
        });
        $("#no-pengenal").keyup(function() {
                
            $.post("<?php echo DATACUSTOMER ?>get_data_customer_autocomplete", "no_pengenal="+$("#no-pengenal").val()+"&&jenis_pengenal="+$("#jenis-pengenal").val(), function(data){
                var arr = $.parseJSON(data);
                if (arr['no_pengenal']!='')
                {
                    $("#country-of-passport").val(arr['country_of_passport']);
                    $("#first-name").val(arr['first_name']);
                    $("#last-name").val(arr['last_name']);
                    $("#status-data-customer").val(arr['id']);
                }
                else
                    $("#status-data-customer").val('-');
            });
        });
        $("#date-pesan-keluar").change(function() {
            var jumlah_hari = parseInt($( "#jumlah-hari" ).val());
            var date_masuk = new Date($("#date-pesan-masuk").val());
            var date_keluar = new Date($("#date-pesan-keluar").val());
            var d = date_masuk.getDate();
            var m = date_masuk.getMonth();
            var y = date_masuk.getYear();
            //                    var NextDate= new Date((1900+y), (d+jumlah_hari), (m));
            //                    alert(NextDate.getYear());
                    
            var tanggal_masuk = (1900+y)+"-"+(m+1)+"-"+d;
            //                    alert(tanggal_masuk);
            var tanggal_keluar= (1900+date_keluar.getYear())+"-"+(date_keluar.getMonth()+1)+"-"+date_keluar.getDate();
            $( "#jumlah-hari" ).val(days_between(date_masuk, date_keluar));
            $('#adjoin-room').empty();
            $.post("<?php echo KAMAR ?>adjoining_room_status", "id_kamar="+id_kamar+"&&tanggalmasuk="+tanggal_masuk+"&&tanggalkeluar="+tanggal_keluar, function(data){
                var arr = $.parseJSON(data);
                var temp = '';
                        
                for(var i=0;i<arr['adjoining'].length;i++)
                {
                                
                    if(id_kamar==arr['all'][i]['id_room1'])
                        temp = arr['all'][i]['id_room2'];
                    else
                        temp = arr['all'][i]['id_room1'];
                    if(arr['adjoining'][i]['used']==true)
                        $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input> waiting list<br/>");
                    else 
                        $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input><br/>");
                                    
                }
                $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='-'>none</input><br/>");
            });  
        });
        $( "#jumlah-hari" ).keyup(function() {
            var jumlah_hari = parseInt($( "#jumlah-hari" ).val());
            var date_masuk = new Date($("#date-pesan-masuk").val())
            var d = date_masuk.getDate();
            var m = date_masuk.getMonth();
            var y = date_masuk.getYear();
            //                    var NextDate= new Date((1900+y), (d+jumlah_hari), (m));
            var NextDate= new Date();
            NextDate.setDate(date_masuk.getDate()+jumlah_hari);
            //                    alert(NextDate.getYear());
            $("#date-pesan-keluar").val((NextDate.getMonth()+1)+"/"+NextDate.getDate()+"/"+(1900+NextDate.getYear()));
            var tanggal_masuk = (1900+y)+"-"+(m+1)+"-"+d;
            //                    alert(tanggal_masuk);
            var tanggal_keluar= (1900+NextDate.getYear())+"-"+(NextDate.getMonth()+1)+"-"+NextDate.getDate();
            $('#adjoin-room').empty();
            $.post("<?php echo KAMAR ?>adjoining_room_status", "id_kamar="+id_kamar+"&&tanggalmasuk="+tanggal_masuk+"&&tanggalkeluar="+tanggal_keluar, function(data){
                var arr = $.parseJSON(data);
                var temp = '';
                        
                for(var i=0;i<arr['adjoining'].length;i++)
                {
                                
                    if(id_kamar==arr['all'][i]['id_room1'])
                        temp = arr['all'][i]['id_room2'];
                    else
                        temp = arr['all'][i]['id_room1'];
                    if(arr['adjoining'][i]['used']==true)
                        $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input> waiting list<br/>");
                    else 
                        $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='"+ temp +"'>"+ arr['adjoining'][i]["nama_kamar1"] +" - "+ arr['adjoining'][i]["nama_kamar2"] +"</input><br/>");
                                    
                }
                $( "#adjoin-room" ).append("<input type='radio' name='adjoining' value='-'>none</input><br/>");
            });  
        });
                    
                
		
    });
</script>
<form action="<?php echo HOME ?>pesan_kamar_process">
    <input type="hidden" id="id-kamar" name="id_kamar" value="<?php echo $data_kamar['id_kamar'] ?>"/>
    <h3 class="center">DATA PELANGGAN</h3>
    <table class="tabelket" width="500px">
        <tr>
            <td class="right" width="35%">Nama kamar</td>
            <td class="left" width="65%">: <?php echo $data_kamar['nama_kamar'] ?></td>
        </tr>
        <tr>
            <td class="right"><?php echo $data_kamar['tipe_letak_kamar'] ?></td>
            <td class="left">: <?php echo $data_kamar['no_letak_kamar'] ?></td>
        </tr>
        <tr>
            <td class="right">Nomor</td>
            <td class="left">: <?php echo $data_kamar['no_kamar'] ?></td>
        </tr>
        <tr>
            <td class="right">Nomor pengenal</td>
            <td class="left">: <input name="no_pengenal" class="panjang" id="no-pengenal" /></td>
        <input name="status_data_customer" type="hidden" id="status-data-customer" value="-" />
        </tr>
        <tr>
            <td class="right">Jenis pengenal</td>
            <td class="left">: <select name="jenis_pengenal" id="jenis-pengenal">
                    <option>KTP</option>
                    <option>SIM</option>
                    <option>Passport</option>
                </select></td>
        </tr>
        <tr>
            <td class="right">Country of passport</td>
            <td class="left">: <input name="country_of_passport" class="panjang" id="country-of-passport" /></td>
        </tr>
        <tr>
            <td class="right">First name</td>
            <td class="left">: <input name="first_name" class="panjang" id="first-name" /></td>
        </tr>
        <tr>
            <td class="right">Last name</td>
            <td class="left">: <input name="last_name" class="panjang" id="last-name" /></td>
        </tr>
        <tr>
            <td class="right">Jumlah hari pesan</td>
            <td class="left">: <input id="jumlah-hari" class="pendek" /></td>
        </tr>
        <tr>
            <td class="right">Tanggal pesan masuk</td>
            <td class="left">: <input type="text" class="datepicker" id="date-pesan-masuk" name="date_pesan_masuk" value="<?php echo ($bulan + 1) . "/" . $tanggal . "/" . date("Y") ?>"></td>
        </tr>
        <tr>
            <td class="right">Tanggal pesan keluar</td>
            <td class="left">: <input type="text" class="datepicker" id="date-pesan-keluar" name="date_pesan_keluar" value="<?php echo($bulan + 1) . "/" . $tanggal . "/" . date("Y") ?>"></td>
        </tr>
        <tr>
            <td class="right">Jumlah orang menginap</td>
            <td class="left">: <input type="text" name="jumlah_huni" class="pendek" /></td>
        </tr>
        <tr>
            <td class="right">Jenis pembayaran</td>
            <td class="left">: <select name="pembayaran_trax">
                    <option>Tunai</option>
                    <option>Check</option>
                </select></td>
        </tr>
        <tr>
            <td class="right">Jumlah</td>
            <td class="left">: <input name="harga_trax" /></td>
        </tr>
        <tr>
            <td class="right">Keterangan</td>
            <td class="left">: <input name="keterangan" /></td>
        </tr>
        <tr>
            <td class="right">Adjoining</td>
            <td class="left">
                <div id="adjoin-room" >: 
                    <?php
                    for ($i = 0; $i < count($all); $i++) {
                        if ($data_kamar['id_kamar'] == $all[$i]['id_room1'])
                            $temp = $all[$i]['id_room2'];
                        else
                            $temp = $all[$i]['id_room1'];
                        if (in_array(array('kamar_id' => $temp), $used)) {
                            ?>
                            <input type="radio" name="adjoining" id="adjoin1" value="<?php echo $temp ?>" />
                            <label for="adjoin1"><?php echo $all[$i]['nama_kamar1'] ?> - <?php echo $all[$i]['nama_kamar2'] ?> waiting list</label>
                            <?php
                        } else {
                            ?>
                            <input type="radio" name="adjoining" id="adjoin2" value="<?php echo $temp ?>" />
                            <label for="adjoin2"><?php echo $all[$i]['nama_kamar1'] ?> - <?php echo $all[$i]['nama_kamar2'] ?></label>
                            <?php
                        }
//            echo $temp;
                    }
//            print_r($used);
                    ?>
                    <input type='radio' name='adjoining' id="adjoin3" value='-' /><label for="adjoin3">none</label>
                </div>
            </td>
        </tr>   
        <tr>
            <td class="right">&nbsp;</td>
            <td class="left"><input type="submit" value="Submit" class="tombol"></td>
        </tr>
    </table>

</form>